<template>
  <div ref="cahrtRef" :class="className" :style="{height:height,width:width}"/>
</template>
<script>
import {getThemeValue} from '@/utils/theme_color.js'
require('echarts/theme/macarons')
import {mapState} from 'vuex'
export default {
  data(){
    return {
      chart: null
    }
  },
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    options:{
      type:Object,
      default(){
        return {}
      }
    }
  },
  watch: {
    options: {
      handler(newValue) {
        this.$nextTick(() => {
          this.chart.setOption(newValue)
      })
      },
      immediate: false,
      deep: true
    },
    sideTheme:{
      handler(newValue){
        this.updateChart();
      }
    }
  },
  computed:{
    ...mapState({
      sideTheme: state => state.settings.sideTheme,
    }),
  },
  mounted() {
    if(this.$el){
      // this.$nextTick(() => {
        this.initChart()
     
        this.updateChart()
      // })
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    // this.chart = null
    window.removeEventListener('resize',() => {
        this.chart.resize()
    })
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$refs.cahrtRef,'macarons')
      this.chart.setOption(this.options, true)
      window.addEventListener('resize',() => {
        this.chart.resize()
    });
    },
    handleResize(){
      this.chart.resize()
    },
    updateChart(){
      const titleThemeDark=getThemeValue('titleThemeDark');
      const DarkThemeStyle={
        title:{
          textStyle:{
            color:titleThemeDark.titleColor
          }
        },
        legend:{
          textStyle:{
            color:titleThemeDark.titleColor
          }
        },
        xAxis:{
          axisLabel:{
            color:'#617898'
          },
          axisLine:{
            show:false
          }
        },
        yAxis:{
          axisLabel:{
            color:'#617898'
          },
          splitLine:{
            lineStyle:{
              color:'rgba(255,255,255,0.15)'
            }
          },
          axisLine:{
            show:false,
          }
        }
      };
      const titleThemeHigh=getThemeValue('titleThemeHigh');
      const HighThemeStyle={
        title:{
          textStyle:{
            color:titleThemeHigh.titleColor
          }
        },
        legend:{
          textStyle:{
            color:titleThemeHigh.titleColor
          }
        },
        xAxis:{
          axisLabel:{
            color:'rgba(0,0,0,0.45)'
          },
          axisLine:{
            show:false
          }
        },
        yAxis:{
          axisLabel:{
            color:' rgba(0,0,0,0.45)'
          },
          axisLine:{
            show:false,
          }
        }
      }
      if(this.sideTheme=='theme-dark'){
        this.chart.setOption(DarkThemeStyle)
      }else{
        this.chart.setOption(HighThemeStyle)
      }

    }

  }
}
</script>
<style>

</style>
